#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/27
# @Author :CJX
# @File   :manual_control.py
from typing import List, Union, Any

import can
import time
import numpy as np
from Injection_valve import injection_port
from Injection_valve import insert_database
from dox_enquiry import dox_command
from detection_relay_board import do_status
from detection_relay_board import di_status
from sql_insert import sql_insert_err
from sql_insert import sql_insert_suc
from threading import Thread
import threading
from i2c import i2c
import os

os.system('sudo ip link set can0 type can bitrate 250000')
os.system('sudo ifconfig can0 up')


def i2c_list():
    i2c_40_time = 0
    i2c_41_time = 0
    i2c_42_time = 0
    i2c_43_time = 0
    while 1:
        global i2c_40, i2c_41, i2c_42, i2c_43
        i2c_list = np.zeros((4, 4), dtype=np.int32)
        i2c_list = i2c()
        i2c_41 = i2c_list[0][0]
        i2c_42 = i2c_list[1][0]
        i2c_43 = i2c_list[2][0]
        i2c_40 = i2c_list[3][0]

        if i2c_40 <= 10 and i2c_40_time == 0:
            dox_command([26], [0])
            i2c_40_time = 1
        elif i2c_40 >= 15 and i2c_40_time == 1:
            dox_command([0], [0])
            i2c_40_time = 0

        if i2c_41 <= 10 and i2c_41_time == 0:
            dox_command([27], [0])
            i2c_41_time = 1
        elif i2c_41 >= 15 and i2c_41_time == 1:
            dox_command([0], [0])
            i2c_41_time = 0

        if i2c_42 <= 10 and i2c_42_time == 0:
            dox_command([26], [0])
            i2c_42_time = 1
        elif i2c_42 >= 15 and i2c_42_time == 1:
            dox_command([0], [0])
            i2c_42_time = 0

        if i2c_43 <= 10 and i2c_43_time == 0:
            dox_command([26], [0])
            i2c_43_time = 1
        elif i2c_40 >= 15 and i2c_43_time == 1:
            dox_command([0], [0])
            i2c_43_time = 0

        if di_location[0:1][0] == '1':
            break


def io_status():
    while 1:
        global var1, var2, data_do, data_di  # 类型为list
        global falling_edge, rising_edge
        global di_location, do_location, err_inquiry, var_err_inquiry
        # global edge, l_s_time  # 长按和短按判断，类型为array元素为True/False
        inquiry_do = can.Message(arbitration_id=0x0201, data=[0x01], is_extended_id=False)  # 问询
        bus.send(inquiry_do)  # 0x0201 DO状态 返回513
        message_do = bus.recv(timeout=1)  # 接收数据
        type_id_do = message_do.arbitration_id  # 返回can报文类型
        if type_id_do == 513:
            # time_float1 = message_do.timestamp          # 浮点数时间戳
            # time_date1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(message_do.timestamp)))  # 转str时间
            # 取回4个字节,预留32通道,bytearray->int->bin(str)->去'0b'->bin->左补32位->反向排列
            data_do = '{:0>32b}'.format(int(bin(int.from_bytes(message_do.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            # print(data_do, type(data_do))
            if var1 != data_do:  # 差异检测，差异写入数据库
                # write_sql(time_date1, type_id_do, data_do)+++++++++++注意排列顺序有修改
                var1 = data_do  # 给中间值赋新值
                do_location = list(data_do)
        time.sleep(0.1)

        err_inquiry = err_inquiry_function(8) + err_inquiry_function(11) + err_inquiry_function(12) + \
                      err_inquiry_function(23) + err_inquiry_function(24) + \
                      err_inquiry_function(25) + err_inquiry_function(26) + err_inquiry_function(27) + \
                      err_inquiry_function(28) + err_inquiry_function(29)
        # err_inquiry供dox_command函数使用，以免复位报警指令
        inquiry_di = can.Message(arbitration_id=0x0301, data=[0x01], is_extended_id=False)
        bus.send(inquiry_di)  # 0x0301  返回769/0x0301 di状态
        message_di = bus.recv(timeout=1)  # 接收数据
        type_id_di = message_di.arbitration_id  # 返回can报文类型
        if type_id_di == 769:
            # time_float2 = message_di.timestamp          # 浮点数时间戳
            # time_date2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(message_di.timestamp)))  # 转str时间
            # 取回4个字,预留32通道,bytearray->int->bin(str)->去'0b'->bin->左补32位->反向排列
            data_di = '{:0>32b}'.format(int(bin(int.from_bytes(message_di.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            # print(data_di, type(data_di))
            if data_di != var2:  # 差异检测，差异写入数据库
                # write_sql(time_date2, type_id_di, data_di)
                for k in range(32):
                    if var2[k] == '0' and data_di[k] == '1':  # 32点上升沿检测,没有检测到不复位
                        edge[0, k] = 1  # 写入边缘检测edge[0]
                        edge[1, k] = 0  # 复位下降沿
                        # time_diff[0, k] = time.time()  # 把时间写入array[0]，上升沿触发时间戳
                    if var2[k] == '1' and data_di[k] == '0':  # 32点下降沿检测,没有检测到不复位
                        edge[1, k] = 1  # 写入边缘检测edge[1]
                        edge[0, k] = 0  # 复位上升沿
                        # time_diff[1, k] = time.time()  # 把时间写入array[1]，下降沿触发时间戳
                    # l_s_time[0, k] = 0 < (time_diff[1, k] - time_diff[0, k]) < 3  # 短脉冲判断
                    # l_s_time[1, k] = 3 < (time_diff[1, k] - time_diff[0, k]) < 10  # 长脉冲判断
                    # print((time_diff[1, k] - time_diff[0, k]))
                var2 = data_di  # 给中间值赋新值
                di_location = list(data_di)  # 字符串转列表输出
                # print('下降沿', edge[1])
                # print('上升沿', edge[0])
                rising_edge = edge[0]
                falling_edge = edge[1]
        time.sleep(0.1)
        if di_location[0:1][0] == '1':
            break


def err_inquiry_function(err_num):  # 供dox_command函数使用，以免复位报警指令
    if do_location[err_num - 1:err_num][0] == "1":
        err_inquiry_num = [err_num]
    else:
        err_inquiry_num = [0]
    return err_inquiry_num


def manual_measurement_s1():
    global err_inquiry, support_failure
    sql_insert_suc("手动测量")
    t = 0
    dox_command([1], err_inquiry)  # 复位并输出DO1
    time.sleep(0.1)
    while t <= 60:
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            break
        if di_location[1:2][0] == "1":
            dox_command([0], err_inquiry)  # 复位DO1
            t = 70
            sql_insert_suc("测量成功")
            break  # 输出测量状态
        else:
            t = t + 1
            time.sleep(1)
    if t > 60 and di_location[1:2][0] == "0":
        sql_insert_err("F-1测量指令故障")  # 可存入数据库
        sql_insert_err("支架故障")
        dox_command([23], err_inquiry)  # 复位DO1,输出do23
        support_failure = 1  # 输出支架故障


def manual_maintenance_s2():
    global err_inquiry, support_failure
    sql_insert_suc("手动检修")
    t = 0
    dox_command([22, 2], err_inquiry)  # 复位并输出DO22,DO2
    time.sleep(0.1)
    while t <= 60:
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            break
        if di_location[2:3][0] == "1":
            dox_command([22], [0])  # 复位DO2
            t = 70
            sql_insert_suc("检修成功")
            pass  # 输出检修状态
        else:
            time.sleep(1)
            t = t + 1
    if t > 60 and di_location[2:3][0] == "0":
        sql_insert_err("F-2检修指令故障")  # 输出检修动作故障
        sql_insert_err("支架故障")
        dox_command([23], err_inquiry)  # 复位DO2,输出do23
        support_failure = 1  # 输出支架故障


def manual_curing_s3():
    global var1, var2, data_do, data_di
    global falling_edge, rising_edge
    global di_location, err_inquiry
    manual_curing_n = 2
    ver = 1  # 1为带自动冲洗，0为不带自动冲洗
    dox_command_t = 0
    if dox_command_t == 0:
        dox_command_t = 1
        dox_command([0], err_inquiry)
    manual_curing_t2 = 0
    manual_curing_t3 = 0
    manual_curing_t5 = 0
    manual_curing_t8 = 0
    manual_curing_t9 = 0
    manual_curing_t10 = 0
    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    if manual_curing_n == 2:
        sql_insert_suc("手动保养中")
        dox_command([10, 22], err_inquiry)
        print("步骤4")
        if ver == 1:
            dox_command([9, 10, 22], err_inquiry)  # 输出do9
            print("步骤6")
            while manual_curing_t2 <= 29:
                manual_curing_t2 = manual_curing_t2 + 1
                print("步骤6时停")
                time.sleep(1)
                if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
                    return
            manual_curing_n = 3
        else:
            manual_curing_n = 3

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    if manual_curing_n == 3:
        print("步骤7")
        dox_command([2, 9, 10, 22], err_inquiry)  # 置位DO2

        while manual_curing_t3 <= 60:
            if di_location[2:3][0] == "1":
                dox_command([9, 10, 22], err_inquiry)  # 复位DO2
                manual_curing_n = 4
                manual_curing_t3 = 70
                sql_insert_suc("检修成功")
                pass  # 输出检修状态
            else:
                time.sleep(1)
                manual_curing_t3 = manual_curing_t3 + 1
        if manual_curing_t3 > 60 and di_location[2:3][0] == "0":
            sql_insert_err("F-2检修指令故障")  # 输出检修动作故障
            dox_command([23], err_inquiry)
            return

    if manual_curing_n == 4:
        print("步骤8")
        dox_command([10, 22], err_inquiry)  # 复位DO9
        injection_port(3)
        time.sleep(2)
        if insert_database() == "034d":  # 查询指令
            time.sleep(2)  # 延时两秒
            manual_curing_n = 5
            sql_insert_suc("切换阀水冲洗位置")
            pass  # 成功

        else:
            sql_insert_err("FX-3切换阀水冲洗位故障")  # 可存入数据库fx-3

    if manual_curing_n == 5:
        dox_command([3, 10, 22], err_inquiry)
        print("步骤9")
        while manual_curing_t5 <= 24:
            print("步骤9时停")
            manual_curing_t5 = manual_curing_t5 + 1
            time.sleep(1)
            if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
                return
        dox_command([10, 22], err_inquiry)
        manual_curing_n = 7

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    if manual_curing_n == 7:
        print("步骤10")
        injection_port(2)
        time.sleep(3)
        if insert_database() == "024d":  # 查询指令
            manual_curing_n = 8
            sql_insert_suc("切换阀清洗位置")
            pass  # 成功
        else:
            sql_insert_err("FX-2切换阀清洗位故障")  # 可存入数据库fx-2

    if manual_curing_n == 8:
        dox_command([4, 10, 22], err_inquiry)
        print("步骤11")
        while manual_curing_t8 <= 7:
            manual_curing_t8 = manual_curing_t8 + 1
            time.sleep(1)
            if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
                return
        manual_curing_n = 9

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    if manual_curing_n == 9:
        dox_command([10, 22], err_inquiry)
        print("步骤12")
        while manual_curing_t9 <= 299:
            manual_curing_t9 = manual_curing_t9 + 1
            time.sleep(1)
            print("步骤12时停")
            if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
                return
        manual_curing_n = 10

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    if manual_curing_n == 10:
        dox_command([7, 10, 22], err_inquiry)
        print("步骤13")
        while manual_curing_t10 <= 7:
            manual_curing_t10 = manual_curing_t10 + 1
            time.sleep(1)
            if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
                return
        manual_curing_n = 11

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    if manual_curing_n == 11:
        print("步骤14")
        dox_command([22], err_inquiry)  # 复位do
        injection_port(0)  # 切换阀复位,X-0指令
        time.sleep(5)
        if insert_database() == "004d":  # 查询指令
            manual_curing_n = 12
            sql_insert_suc("切换阀初始化位置")
            pass  # 成功
        else:
            sql_insert_err("FX-0切换阀初始化故障")  # 可存入数据库

    if manual_curing_n == 12:
        #  "输出养护中"
        print("步骤15")
        sql_insert_suc("手动保养结束")
        pass  #


def manual_cleaning_s4():
    global err_inquiry, support_failure
    sql_insert_suc("手动冲洗腔体中")
    dox_command([22], err_inquiry)  # 输出do22
    injection_port(3)  # 切换阀复位,X-3指令

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return

    time.sleep(3)
    if insert_database() == "034d":  # 查询指令
        sql_insert_suc("切换阀水冲洗位置")
        pass  # 成功
    else:
        sql_insert_err("FX-3切换阀水冲洗位故障")  # 可存入数据库
    dox_command([22, 3], err_inquiry)  # 输出do3
    while 1:
        if di_location[5:6][0] == "0":
            injection_port(0)  # 切换阀复位,X-3指令
            dox_command([22], err_inquiry)  # 复位DO3
            time.sleep(3)
            if insert_database() == "004d":  # 查询指令
                sql_insert_suc("切换阀复位成功")
                sql_insert_suc("手动冲洗腔体结束")
                pass  # 成功
                return
            else:
                sql_insert_err("切换阀复位故障FX-0")  # 可存入数据库
                return


def manually_replenish_cleaning_fluid_s5():
    global i2c_40, i2c_41, i2c_42, i2c_43
    global falling_edge, rising_edge, edge
    global err_inquiry, support_failure
    sql_insert_suc("补充清洗液中")
    dox_command([6, 17], err_inquiry)
    while 1:
        print(i2c_40)
        if i2c_40 >= 200 or falling_edge[6:7][0] == 1:
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            dox_command([0], err_inquiry)
            sql_insert_suc("清洗液补充结束")
            break
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            break


def manually_replenish_protective_fluid_s6():
    global i2c_40, i2c_41, i2c_42, i2c_43
    global falling_edge, rising_edge, edge
    global err_inquiry, support_failure
    sql_insert_suc("补充保护液中")
    dox_command([7, 18], err_inquiry)  # 输出do7，do18
    while 1:
        if i2c_41 >= 200 or falling_edge[7:8][0] == 1:
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            dox_command([0], err_inquiry)
            sql_insert_suc("保护液补充结束")
            break
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            break


def manually_replenish_buffer_4_s7():
    global i2c_40, i2c_41, i2c_42, i2c_43
    global falling_edge, rising_edge, edge
    global err_inquiry, support_failure
    dox_command([15, 19], err_inquiry)
    while 1:
        if i2c_42 >= 200 or falling_edge[11:12][0] == 1:
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            dox_command([0], err_inquiry)
            break
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            break


def delivery_buffer_4_s8():
    global err_inquiry, support_failure
    delivery_buffer_4_s8_t = 0
    sql_insert_suc("输送一次 4.00 缓冲液")
    dox_command([0], err_inquiry)
    injection_port(1)
    time.sleep(2)
    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return
    dox_command([13], err_inquiry)
    if insert_database() == "014d":  # 查询指令
        sql_insert_suc("切换阀PH=4位置")
        pass  # 成功
    else:
        sql_insert_err("FX-1切换阀PH=4位故障")  # 可存入数据库
    while delivery_buffer_4_s8_t < 8:
        delivery_buffer_4_s8_t = delivery_buffer_4_s8_t + 1
        time.sleep(1)
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            return
        print(delivery_buffer_4_s8_t)
    dox_command([0], err_inquiry)
    injection_port(0)
    time.sleep(2)
    if insert_database() == "004d":  # 查询指令
        sql_insert_suc("切换阀初始化位置")
        pass  # 成功
    else:
        sql_insert_err("FX-0切换阀初始化故障")  # 可存入数据库


def manually_replenish_buffer_6_s9():
    global i2c_40, i2c_41, i2c_42, i2c_43
    global falling_edge, rising_edge, edge
    global err_inquiry, support_failure
    dox_command([16, 20], err_inquiry)
    while 1:
        if i2c_43 >= 200 or falling_edge[12:13][0] == 1:
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            dox_command([0], err_inquiry)
            break
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            break


def delivery_buffer_6_s10():
    global err_inquiry, support_failure
    delivery_buffer_6_s10_t = 0
    sql_insert_suc("输送一次 6.86 缓冲液")
    dox_command([0], err_inquiry)
    injection_port(4)
    time.sleep(2)

    if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
        return
    dox_command([14], err_inquiry)

    if insert_database() == "044d":  # 查询指令
        sql_insert_suc("切换阀PH=6位置")
        pass  # 成功
    else:
        sql_insert_err("FX-4切换阀PH=6位故障")  # 可存入数据库
    while delivery_buffer_6_s10_t < 8:
        delivery_buffer_6_s10_t = delivery_buffer_6_s10_t + 1
        time.sleep(1)
        if di_location[0:1][0] == '1' or di_location[30:31][0] == '1':
            return
        print(delivery_buffer_6_s10_t)
    dox_command([0], err_inquiry)
    injection_port(0)
    time.sleep(2)
    if insert_database() == "004d":  # 查询指令
        sql_insert_suc("切换阀初始化位置")
        pass  # 成功
    else:
        sql_insert_err("FX-0切换阀初始化故障")  # 可存入数据库


def manual_control():
    maintain_t = 0  # 用于保养的计数
    buffer_4_t = 0  # 用于缓冲液4的计数
    buffer_6_t = 0  # 用于缓冲液6的计数
    reset_timing = 0  # 用于手动转自动后首次复位
    reset_t = 0  # 用于复位按钮的计数
    while 1:
        global falling_edge, rising_edge, di_location
        global i2c_40, i2c_41, i2c_42, i2c_43, edge, err_inquiry

        if reset_timing == 0:
            reset_timing = 1
            dox_command([30], err_inquiry)
            injection_port(0)
            time.sleep(3)
            if insert_database() == "004d":
                sql_insert_suc("切换阀复位成功")
            else:
                sql_insert_err("切换阀复位失败")
            dox_command([0], err_inquiry)

        if di_location[3:4][0] == "1" and di_location[1:2][0] == "0":
            manual_measurement_s1()
            print("测量")

        if di_location[4:5][0] == "1" and di_location[2:3][0] == "0":
            manual_maintenance_s2()
            print("检修")

        if 0 < maintain_t <= 30:
            maintain_t = 0
            manual_curing_s3()
            print("保养")

        if maintain_t > 30:
            maintain_t = 0
            manual_cleaning_s4()
            print("手动清洗")

        while di_location[5:6][0] == "1" and maintain_t <= 30:
            maintain_t = maintain_t + 1
            print(maintain_t)
            time.sleep(0.1)

        if falling_edge[6:7][0] == 1:  # 检测DI6的下降沿并复位
            print(falling_edge)
            print('手动补充清洗液')
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            manually_replenish_cleaning_fluid_s5()

        if falling_edge[7:8][0] == 1:  # 检测DI7的下降沿并复位
            print('手动补充保护液')
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            manually_replenish_protective_fluid_s6()

        if 0 < buffer_4_t <= 30:
            buffer_4_t = 0
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            manually_replenish_buffer_4_s7()
            print("补充缓冲液4")

        if buffer_4_t > 30:
            buffer_4_t = 0

            delivery_buffer_4_s8()
            print("输送缓冲液4")

        while di_location[11:12][0] == "1" and buffer_4_t <= 30:
            buffer_4_t = buffer_4_t + 1
            print(buffer_4_t)
            time.sleep(0.1)

        if 0 < buffer_6_t <= 30:
            buffer_6_t = 0
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            edge = np.zeros(64, np.int8).reshape(2, 32)
            manually_replenish_buffer_6_s9()
            print("补充缓冲液6")

        if buffer_6_t > 30:
            buffer_6_t = 0
            delivery_buffer_6_s10()
            print("输送缓冲液6")

        while di_location[12:13][0] == "1" and buffer_6_t <= 30:
            buffer_6_t = buffer_6_t + 1
            print(buffer_6_t)
            time.sleep(0.1)

        if di_location[0:1][0] == '1':
            break

        while di_location[30:31][0] == "1" and reset_t <= 30:
            reset_t = reset_t + 1
            print(reset_t)
            time.sleep(0.1)

        if 0 < reset_t < 30:
            reset_t = 0
            dox_command([0], err_inquiry)
            injection_port(0)
            time.sleep(3)
            if insert_database() == "004d":
                sql_insert_suc("切换阀复位成功")
            else:
                sql_insert_err("切换阀复位失败")

        if reset_t > 30:
            reset_t = 0
            injection_port(0)
            time.sleep(3)
            if insert_database() == "004d":
                sql_insert_suc("切换阀复位成功")
            else:
                sql_insert_err("切换阀复位失败")
            dox_command([0], [0])


threads = []
t1 = threading.Thread(target=io_status)
threads.append(t1)

t2 = threading.Thread(target=manual_control)
threads.append(t2)

t3 = threading.Thread(target=i2c_list)
threads.append(t3)

if __name__ == "__main__":
    # message_di = 0
    global manual_control_status, remote_maintenance_status, automatic_cleaning_status, do_location
    global err_inquiry, var_err_inquiry, support_failure
    support_failure = 0  # 支架状态判断0
    var1, var2 = '00000000000000000000000000000000', '00000000000000000000000000000000'
    data_do, data_di = 0, 0
    var_err_inquiry = [0]
    err_inquiry = ['0', '0', '0', '0', '0', '0', '0', '0']
    do_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    di_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rising_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    edge = np.zeros(64, np.int8).reshape(2, 32)  # DI上升/下降沿检测,edge[0,0]第1个up，edge[1,0]第1个down
    time_diff = np.zeros(64, np.float64).reshape(2, 32)  # DI上升/下降沿的触发时间
    l_s_time = np.zeros(64, np.int8).reshape(2, 32)  # 短按判断l_s_time[0]|长按判断l_s_time[1]
    bus = can.Bus(interface='socketcan', channel='can0', receive_own_messages=eval('False'))
    while di_location[0] == "0":
        for t in threads:
            t.start()
        for t in threads:
            t.join()
        if di_location[0:1][0] == '1':
            break
